﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProjectManagement;

namespace GeneratorManager
{
    public partial class FrmNewProject : DevExpress.XtraEditors.XtraForm
    {
        private string _lastExistPath;

        public FrmNewProject()
        {
            InitializeComponent();
            InitializeValues();
        }

        #region Methods
            private void InitializeValues()
            {
                txtPath.Text = String.Format("{0}\\Projects\\", Application.StartupPath);
                _lastExistPath = String.Format("{0}\\Projects\\", Application.StartupPath);
                if (!System.IO.Directory.Exists(_lastExistPath))
                {
                    System.IO.Directory.CreateDirectory(_lastExistPath);
                }
            }
        #endregion

        #region ControlEvents
            private void BtnFindPathClick(object sender, EventArgs e)
            {
                try
                {
                    folderBrowserDialog.SelectedPath = _lastExistPath;
                    folderBrowserDialog.ShowDialog();
                    if (System.IO.Directory.Exists(folderBrowserDialog.SelectedPath))
                    {
                        txtPath.Text = folderBrowserDialog.SelectedPath;
                        _lastExistPath = folderBrowserDialog.SelectedPath;
                    }
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error al buscar el directorio. {0}", ex.Message));
                }
            }

            private void BtnSaveAndContinueClick(object sender, EventArgs e)
            {
                try
                {
                    Project newProject = PMHelper.CreateProject(txtProjectName.Text, string.Format("{0}{1}", txtPath.Text, txtProjectName.Text));
                    DatabaseManagement.DBManager.InitializeInstanceAndDatabase(); //TODO: Borrar cuando se deje de utilizar.
                    DatabaseManagement.DBManager.SaveDatabases(ProjectManagement.PMHelper.CurrentProject.Path);
                    MessageBox.Show(
                        string.Format("Se ha creado correctamente el Proyecto: {0} en el directorio: {1}.",
                                      newProject.Name, newProject.Path), "Whoever - Información.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
            }

            private void TxtPathLeave(object sender, EventArgs e)
            {
                try
                {
                    if (System.IO.Directory.Exists(txtPath.Text))
                    {
                        txtPath.ForeColor = Color.Black;
                        _lastExistPath = txtPath.Text;
                        if (txtPath.Text.LastIndexOf("\\") != txtPath.Text.Length - 1)
                        {
                            txtPath.Text = string.Format("{0}\\", txtPath.Text);
                        }
                    }
                    else
                    {
                        txtPath.ForeColor = Color.Red;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error: {0}", ex.Message));
                }
            }
        #endregion
    }
}